package handler

import (
	"log"
	feeds "git.oschina.net/fanbuchi/xgggh/feeds/srv/proto/feed"
	"golang.org/x/net/context"
	service "git.oschina.net/fanbuchi/xgggh/feeds/service/feed"
	oauthCli "git.oschina.net/fanbuchi/xgggh/oauth2/cli"
	usersClient "git.oschina.net/fanbuchi/xgggh/users/cli"
	"git.oschina.net/fanbuchi/xgggh/users/proto/user/contact"
)

type Feed struct{
	service.Feeds;
}

//micro query com.xgggh.srv.feeds Feed.Create '{"userName": "15108280907", "feedUserName": "15108280908","feed": "哈哈哈测试,测试内容!","where": "四川省,成都市,高新区,天府三街22号","geo": "30.1244,104.345666"}'
func (handler *Feed) Create(ctx context.Context, req *feeds.Feed, rsp *feeds.CreateRsp) error {
	// oauth2Service
	revokeRsp,err:=oauthCli.Revoke(ctx)
	if err !=nil{
		return err
	}
	err=handler.Feeds.Create(revokeRsp,req)
	if err!=nil {
		rsp.ErrorDesc=err.Error()
	}else{
		rsp.Ok = true
	}
	return nil
}

//micro query com.xgggh.srv.feeds Feeds.GetFeed '{"feedId":"1"}'
func (handler *Feed) Query(ctx context.Context, req *feeds.QueryReq, rsp *feeds.QueryRsp) error {
	_feed,err:=handler.Feeds.Query(req)
	if err!=nil {
		rsp.ErrorDesc=err.Error()
	}else{
		rsp.Ok = true
		rsp.Data= _feed
	}
	return nil
}

func (handler *Feed) QueryList(ctx context.Context, req *feeds.QueryListReq, rsp *feeds.QueryListRsp) error {
	_feed,err:=handler.Feeds.QueryListByMobile(req)
	if err!=nil {
		rsp.ErrorDesc=err.Error()
	}else{
		rsp.Ok = true
		rsp.DataList= _feed
	}
	return nil
}

func (handler *Feed) Delete(ctx context.Context, req *feeds.DeleteReq, rsp *feeds.Response) error {
	err:=handler.Feeds.Delete(req)
	if err!=nil {
		rsp.ErrorDesc=err.Error()
	}else{
		rsp.Ok = true
	}
	return nil
}


func (handler *Feed) Update(ctx context.Context, req *feeds.Feed, rsp *feeds.Response) error {
	// oauth2Service
	revokeRsp,err:=oauthCli.Revoke(ctx)
	if err !=nil{
		return err
	}
	err=handler.Feeds.Update(revokeRsp,req)
	if err!=nil {
		rsp.ErrorDesc=err.Error()
	}else{
		rsp.Ok = true
	}
	return nil
}

//home
//取评价的用户最新的一条
//SELECT * FROM (SELECT id,feed_mobile,feed_name, feed, rank() OVER (PARTITION BY feed_mobile ORDER BY id DESC) AS topnum FROM feeds.feeds) WHERE topnum = 1;
//SELECT id,feed_mobile,feed_name,feed FROM feeds.feeds WHERE feed_mobile = '15559837173' ORDER BY id DESC;
 func (handler *Feed) Home(ctx context.Context, req *feeds.SearchReq, rsp *feeds.SearchRsp) error {
	 //权限验证
	 _ouathRsp,err:=oauthCli.Revoke(ctx)
	 if err !=nil{
		 return err
	 }
	//search params
	 //limit,offset:=page.Page(req.PageNumber,req.ResultPerPage)
	 params:=com_xgggh_srv_users_contact.Contact{AccountId:_ouathRsp.Data.AccountId}
	 contactSearch:=com_xgggh_srv_users_contact.SearchReq{PageNumber:req.PageNumber,
		 ResultPerPage:req.ResultPerPage,
		 Params:&params,
	 }
	 contact,err:=usersClient.MyContact(ctx,&contactSearch)
	 if contact ==nil || contact.DataList==nil ||len(contact.DataList)<=0 {

	 }
	 _feeds,err:=handler.Feeds.Home(contact.DataList,req)
	 if err ==nil{
		 rsp.DataList=_feeds
	 }
	 return  err
}

//micro query com.xgggh.srv.feeds Feed.Search '{"page":1,"perPage":18}'
func (e *Feed) Search(ctx context.Context, req *feeds.SearchReq, rsp *feeds.SearchRsp) error {
	//权限验证
	oauthRsp,err:=oauthCli.Revoke(ctx)
	if err !=nil{
		rsp.ErrorDesc = err.Error()
		return nil
	}

	_feeds,err:=e.Feeds.Search(oauthRsp.Data.AccountId,req)

	if err !=nil{
		rsp.ErrorDesc = err.Error()
	}else{
		rsp.Ok=true
		rsp.DataList=_feeds
	}
	return  nil
}

// Stream is a server side stream handler called via client.Stream or the generated client code
func (e *Feed) Stream(ctx context.Context, req *feeds.StreamingRequest, stream feeds.Feeds_StreamStream) error {
	log.Printf("Received Feeds.Stream request with count: %d", req.Count)

	for i := 0; i < int(req.Count); i++ {
		log.Printf("Responding: %d", i)
		if err := stream.Send(&feeds.StreamingResponse{
			Count: int64(i),
		}); err != nil {
			return err
		}
	}

	return nil
}

// PingPong is a bidirectional stream handler called via client.Stream or the generated client code
func (e *Feed) PingPong(ctx context.Context, stream feeds.Feeds_PingPongStream) error {
	for {
		req, err := stream.Recv()
		if err != nil {
			return err
		}
		log.Printf("Got ping %v", req.Stroke)
		if err := stream.Send(&feeds.Pong{Stroke: req.Stroke}); err != nil {
			return err
		}
	}
}
